package com.bjut.dao.impl;

import java.util.List;

import com.bjut.dao.AttendanceInfoDao;
import com.bjut.domain.AttendanceInfo;
import com.bjut.domain.Position;

public class AttendanceInfoDaoImpl extends HRglxtHibernateDaoSupport implements
		AttendanceInfoDao {

	public void save(AttendanceInfo attendanceInfo) {
		getHibernateTemplate().save(attendanceInfo);

	}

	public void update(AttendanceInfo attendanceInfo) {
		getHibernateTemplate().merge(attendanceInfo);

	}

	public AttendanceInfoDaoImpl find(int id) {
		return (AttendanceInfoDaoImpl) getHibernateTemplate().get(
				AttendanceInfoDaoImpl.class, id);

	}

	public void delete(int id) {
		getHibernateTemplate().delete(
				getHibernateTemplate().get(AttendanceInfoDaoImpl.class, id));

	}

	public void delete(AttendanceInfo attendanceInfo) {
		getHibernateTemplate().delete(attendanceInfo);
	}

	public AttendanceInfo findByEmpId(int id) {
		List result=getHibernateTemplate().find("from AttendanceInfo as p where p.empid = ?" , 
				id);
		if (result != null && result.size() > 0)
			return (AttendanceInfo) result.get(0);
		else 
			return null;
	}

	public AttendanceInfo findById(int id) {
		return (AttendanceInfo) getHibernateTemplate().get(AttendanceInfo.class, id);
	}

	public AttendanceInfo findByYearAndMonthByPage(int year,int month,int pageNo, int pageSize) {
		
		if (pageNo < 1)
		{
			return null;
		}
		int offset = (pageNo - 1) * pageSize;
		
		return (AttendanceInfo) findByPage("from AttendanceInfo as p where p.year="+year+" and p.month ="+month+" order by p.id", offset , pageSize);
	}

	public AttendanceInfo get(int id) {
		return (AttendanceInfo) getHibernateTemplate().get(AttendanceInfo.class, id);
	}

	public long getCount() {
		List attendanceInfoList = getHibernateTemplate().find("select count(p.id) from AttendanceInfo as p");
		if (attendanceInfoList == null)
		{
			return 0;
		}
		return (Long)attendanceInfoList.get(0);
	}
	

	
	public List<AttendanceInfo> findAllByPage(int pageNo, int pageSize) {
		if (pageNo < 1)
		{
			return null;
		}
		int offset = (pageNo - 1) * pageSize;
		return findByPage("from AttendanceInfo as p order by p.id", offset , pageSize);
	}

	

	public List<Position> findByDepartmentAndPage(int department_id,
			int pageNo, int pageSize) {
		if (pageNo < 1)
		{
			return null;
		}
		int offset = (pageNo - 1) * pageSize;
		return findByPage("from AttendanceInfo as p where department_id=? order by p.id",department_id, offset , pageSize);
	}

	public long getCountByDepartment(int department_id) {
		List attendanceInfoList = getHibernateTemplate().find("select count(p.id) from AttendanceInfo as p where department_id=?",department_id);
		if (attendanceInfoList == null)
		{
			return 0;
		}
		return (Long)attendanceInfoList.get(0);
	}

}
